package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.GoodsDetailsBean
import com.onlyphoto.onlymall.mvp.view.activity.CouponActivity
import com.onlyphoto.onlymall.mvp.view.activity.GoodsDetailsActivity
import com.onlyphoto.onlymall.utils.CollectionUtils
import com.onlyphoto.onlymall.widget.BannerLayout

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-07-09.
 */
class DetailsHeaderBinder : ItemViewBinder<GoodsDetailsBean, DetailsHeaderBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_details_header, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: GoodsDetailsBean) {
        viewHolder.update(bean)

        viewHolder.rlSpecifications?.setOnClickListener{
            styleDialog?.show()
        }
        viewHolder.rlOffer?.setOnClickListener{
            viewHolder.itemView.context.startActivity(Intent(viewHolder.itemView.context, CouponActivity::class.java)
                    .putExtra("isStore", true))
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var proImages: BannerLayout? = null
        var tvName: TextView? = null
        var giftName: TextView? = null
        var priceNow: TextView? = null
        var priceOld: TextView? = null
        var carriageFee: TextView? = null
        var salesVolume: TextView? = null
        var rlSpecifications: RelativeLayout? = null
        var rlOffer: RelativeLayout? = null

        init {
            proImages = itemView.findViewById(R.id.pro_images)
            tvName = itemView.findViewById(R.id.tv_name)
            giftName = itemView.findViewById(R.id.gift_name)
            priceNow = itemView.findViewById(R.id.price_now)
            priceOld = itemView.findViewById(R.id.price_old)
            carriageFee = itemView.findViewById(R.id.carriage_fee)
            salesVolume = itemView.findViewById(R.id.sales_volume)
            rlSpecifications = itemView.findViewById(R.id.rl_specifications)
            rlOffer = itemView.findViewById(R.id.rl_offer)
        }

        @SuppressLint("SetTextI18n")
        fun update(bean: GoodsDetailsBean) {
            proImages?.setImageLoader(GoodsDetailsActivity.GlideImageLoader())
            val urls = java.util.ArrayList<String>()
            if (!CollectionUtils.isEmpty(bean.front_img_arr)) {
                for (url in bean.front_img_arr) {
                    urls.add(url)
                }
            }
            proImages?.setViewUrls(urls)

            tvName?.text = bean.title
            if (bean.gift_set == 1) {
                giftName?.visibility = View.VISIBLE
                giftName?.text = "赠品：${bean.gift_arr.title}"
            }
            priceNow?.text = "¥ ${bean.price_now}"
            priceOld?.text = "¥ ${bean.price_org}"
            priceOld?.paint?.flags = Paint.STRIKE_THRU_TEXT_FLAG
            if (bean.freight_set == 0) {
                carriageFee?.text = "快递：包邮"
            } else {
                carriageFee?.text = "快递：${bean.freight_set}"
            }
            salesVolume?.text = "销量：${bean.sale_num}"
        }
    }

    var styleDialog: StyleDialog? = null

    interface StyleDialog{
        fun show()
    }

    fun showStyleDialog(styleDialog: StyleDialog) {
        this.styleDialog = styleDialog
    }
}
